package cn.autumnorange.app.common.consumer.rpc.lock.aop;

import cn.autumnorange.app.common.consumer.rpc.ConsumerRPCService;
import cn.autumnorange.app.common.consumer.rpc.lock.strategy.RpcidAnnotationnStrategy;
import cn.autumnorange.app.common.rpc.anntation.AnnotationContext;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.redisson.api.RedissonClient;

import java.lang.reflect.Parameter;

@Slf4j
public class RPCKeyLockMethodInterceptor implements MethodInterceptor {

  private AnnotationContext annotationContext = AnnotationContext.getAnnotationContext();

  public RPCKeyLockMethodInterceptor(
          RedissonClient redissonClient, ConsumerRPCService consumerRPCService, long connectTimeout, long readTimeout,int maxAutoRetries,int maxAutoRetriesNextServer) {
    RpcidAnnotationnStrategy rpcidAnnotationnStrategy =
        new RpcidAnnotationnStrategy(redissonClient, consumerRPCService,connectTimeout, readTimeout,maxAutoRetries,maxAutoRetriesNextServer);
    annotationContext.putAnnotationStrategy(
        RpcidAnnotationnStrategy.ANNOTATIONSTRATEGY, rpcidAnnotationnStrategy);
  }

  @Override
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {

    //      annotationContext.putAnnotationStrategy(
    //              RedisCacheTarget.class.getName(), redisCacheReadAnnotationnStrategy);
    //       return methodInvocation.proceed();

    ////        testAOP=new TestAOP(redissonClient);
    ////        testAOP.setAnnotation(annotation);
    //////
    // testAOP.setRedissonClient(applicationContext.getBean(RedissonClient.class));
    ////        return testAOP.annotationTargetHandle(methodInvocation);

    //        // 获取注解参数 getType返回Class类型
    Parameter[] parameters = methodInvocation.getMethod().getParameters();
    Object[] arguments=methodInvocation.getArguments();
                                          methodInvocation.getArguments(); //
//                    // 此处joinPoint的实现类是MethodInvocationProceedingJoinPoint
//                    //          MethodSignature methodSignature = (MethodSignature) signature; // 获取参数名
    for(int i=0;i<arguments.length;i++){
      Object arg=arguments[i];
      log.info("argument: " +arg);
    }
    for (Parameter parameter : parameters) {
      Class<?> classtype = parameter.getType();
      if (classtype != null) {
        log.info("classtype: " + classtype.getName());
        log.debug("classtype: " + classtype.getName());
        log.error("classtype: " + classtype.getName());

        log.warn("classtype: " + classtype.getName());
      }
      log.info("parameter " + parameter.getName());
    }

    return annotationContext.annotationTargetHandle(
        RpcidAnnotationnStrategy.ANNOTATIONSTRATEGY, methodInvocation);
    //
    //        String cacheNameAndType = redisCacheTargt.cacheNameAndType();
    //        String[] cacheNameAndTypeArray = cacheNameAndType.split("#");
    //        // 获取方法参数名
    //        ParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
    //        String dataType = cacheNameAndTypeArray[0];
    //        // 获取redis缓存数据
    //        // 读缓存加读锁 ,获取不到读数据库加写锁将数据库数据写缓存
    //        //     写数据加写锁 写数据库,写缓存
    ////        RReadWriteLock rReadWriteLock =
    ////                redissonClient.getReadWriteLock("testlock" + cacheNameAndTypeArray[1]);
    ////
    ////        RLock readRLock = rReadWriteLock.readLock();
    ////        //    // 对指定参数加读锁,有写锁时会被卡住
    ////        readRLock.lock(5000, TimeUnit.MILLISECONDS);
    //        return  methodInvocation.proceed();
  }
  //
  //    public RedissonClient getRedissonClient() {
  //        return redissonClient;
  //    }
  //
  //    public void setRedissonClient(RedissonClient redissonClient) {
  //        this.redissonClient = redissonClient;
  //    }
}
